
let weekArr = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]

//获取当前时间
const getNowFormatDate = function (format = "yyyy-MM-dd HH:mm") {
    switch (format) {
        case "yyyy-MM-dd HH:mm":
            return setYYYYMMdd() + " " + setHHmm(true);
            break;
        case "yyyy-MM-dd":
            return setYYYYMMdd();
            break;
        case "MM-dd":
            return setMMdd();
            break;
        case "HH:mm":
            return setHHmm();
            break;
        case "HH:mm:int":
            return setHHmm(true);
            break;
        case "week":
            return setWeek();
            break;
    }
}

//str：字符串，addYear：添加年 addMonth：添加月 addDay：添加日
const setYYYYMMdd = function (str = "", addYear = 0, addMonth = 0, addDay = 0) {
    let date = str ? new Date(str.replace(/\-/g, "/")) : new Date();
    date.setFullYear(date.getFullYear() + addYear, date.getMonth() + addMonth, date.getDate() + addDay);
    let seperator1 = "-";
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    let currentdate = year + seperator1 + month + seperator1 + strDate;
    return str.split(" ").length == 2 ? currentdate + " " + setHHmm(true, str) : currentdate;
};
//str:字符串，addYear：添加年
const setYYYY = function (str = "", addYear = 0) {
  let date = str ? new Date(str.replace(/\-/g, "/")) : new Date();
  date.setFullYear(date.getFullYear() + addYear, date.getMonth() , date.getDate() );
  let year = date.getFullYear();
  return year;
};
//str：字符串 addMonth：添加月 addDay：添加日
const setMMdd = function (str = "", addMonth = 0, addDay = 0) {
    let date = str ? new Date(str.replace(/\-/g, "/")) : new Date();
    date.setMonth(date.getMonth() + addMonth, date.getDate() + addDay);
    let month = date.getMonth() + 1;
    let strDate = date.getDate();

    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    let currentdate = month + "月" + strDate + "日";
    return currentdate;
}

//isInt:是否取整 addhour：添加小时 addMinute：添加分钟 str:传入字符串时间
const setHHmm = function (isInt = false, str = "", addhour = 0, addMinute = 0) {
    let date = str ? new Date(str.replace(/\-/g, "/")) : new Date();
    date.setHours(date.getHours() + addhour, date.getMinutes() + addMinute)
    let seperator = ":";
    let hour = date.getHours();
    let minutes = date.getMinutes();
    if (isInt) {
        if (minutes == 0) {
            minutes = 0;
        }
        else if (minutes > 30) {
            hour++;
            minutes = 0;
        }
        else {
            minutes = 30;
        }
    }
    if (hour >= 0 && hour <= 9) {
        hour = "0" + hour;
    }
    if (minutes >= 0 && minutes <= 9) {
        minutes = "0" + minutes;
    }
    let currentdate = hour + seperator + minutes;
    return currentdate;
}

//获取星期
const setWeek = function (day = "") {
    let date;
    if (day) {
        date = new Date(day.replace(/\-/g, "/"));
    }
    else {
        date = new Date();
    }
    return weekArr[date.getDay()];
}

//获取时间戳
const getTime = function (str = "") {
    if (str) {
        return new Date(str.replace(/\-/g, "/")).getTime()
    }
    else {
        return new Date().getTime()
    }
}
//获取月份
const getMonth = function (str = "") {
    if (str) {
        return new Date(str.replace(/\-/g, "/")).getMonth()
    }
    else {
        return new Date().getMonth()
    }
}
//获取某一天
const getDate = function (str = "") {
    if (str) {
        return new Date(str.replace(/\-/g, "/")).getDate()
    }
    else {
        return new Date().getDate()
    }
}
//获取周几
const getDay = function (str = "") {
    if (str) {
        return new Date(str.replace(/\-/g, "/")).getDay()
    }
    else {
        return new Date().getDay()
    }
}

//获取小时
const getHours = function (str = "") {
    if (str) {
        return new Date(str.replace(/\-/g, "/")).getHours()
    }
    else {
        return new Date().getHours()
    }
}
//获取分钟
const getMinutes = function (str = "") {
    if (str) {
        return new Date(str.replace(/\-/g, "/")).getMinutes()
    }
    else {
        return new Date().getMinutes()
    }
}

// js 格式化 date 对象，输出格式为 yyyy-MM-dd HH:mm:ss 字符串
const jsDateFormatter = function( dateInput ) {  // dateInput 是一个 js 的 Date 对象
        var year = dateInput.getFullYear();
        var month = dateInput.getMonth() + 1;
        var theDate = dateInput.getDate();

        var hour = dateInput.getHours();
        var minute = dateInput.getMinutes();
        var second = dateInput.getSeconds();

        if ( month < 10 ) {
            month = '0' + month;
        }

        if ( theDate < 10 ) {
            theDate = '0' + theDate;
        }

        if ( hour < 10 ) {
            hour = '0' + hour;
        }

        if ( minute < 10 ) {
            minute = '0' + minute;
        }

        if ( second < 10 ) {
            second = '0' + second;
        }

        return year +"-"+ month +"-" + theDate + " "+ hour +":"+ minute +":"+ second;
}
export {
    getNowFormatDate,
    setYYYYMMdd,
    setYYYY,
    setMMdd,
    setHHmm,
    setWeek,
    getTime,
    getMonth,
    getDate,
    getDay,
    getHours,
    getMinutes,
    jsDateFormatter,
}
